﻿using Consul;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BasicData.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ConsulController : ControllerBase
    {
        private readonly IConsulClient _consulClient;

        public ConsulController(IConsulClient consulClient)
        {
            _consulClient = consulClient;
        }

        // 获取健康服务
        [HttpGet("health/services")]
        public async Task<IActionResult> GetHealthyServices([FromQuery] string serviceName)
        {
            var health = await _consulClient.Health.Service(serviceName, tag: null, passingOnly: true);
            return Ok(health.Response.Select(x => new {
                x.Service.ID,
                x.Service.Service,
                x.Service.Address,
                x.Service.Port,
                x.Checks
            }));
        }
    }
}
